#include "elec_show.h"

Elec_Show::Elec_Show(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    ui_Init();
    connect_Init();
}
void Elec_Show::ui_Init()
{

    for(int i=0;i<3;i++)//创建主屏幕显示的按键
    {
         theLabel[i] = new QLabel(this);
    }
    theLabel[0]->setStyleSheet("QLabel{ image: url(:/pic/logo.png); background-color: rgb(20, 170, 225);}"    );
    theLabel[0]->setStyleSheet("QLabel{"
                          + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
    theLabel[1]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
    theLabel[2]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

   theLabel[0]->setGeometry(0,0,640,60);//设定每隔放置一个按键
   theLabel[1]->setGeometry(0,0,300,60);//设定每隔放置一个按键
   theLabel[2]->setGeometry(340,0,300,60);//设定每隔放置一个按键
   theLabel[0]->setText("电磁阀");

    for(int i=0;i<4;i++)//创建主屏幕显示的按键
    {
         theBtn[i] = new QPushButton(this);
         btnlist_ui.append(theBtn[i]);
         theBtn[i]->setFixedSize(120,90);

}
theBtn[0]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/Mainwindow/运行.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/Mainwindow/运行2.png); /* 背景图片 */"+"}");
theBtn[1]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/Airflow_mode/等待.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/Airflow_mode/等待_2.png); /* 背景图片 */"+"}");
theBtn[2]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");
theBtn[3]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");



btnlist_ui.at(0)->setText(tr("内部电磁阀"));
btnlist_ui.at(1)->setText(tr("外部电磁阀"));
btnlist_ui.at(2)->setText(tr("三通阀"));
btnlist_ui.at(3)->setText(tr("返回"));

QGridLayout *theLayout = new QGridLayout();
theLayout->addWidget(btnlist_ui.at(0), 1, 0);
theLayout->addWidget(btnlist_ui.at(1), 1, 1);
theLayout->addWidget(btnlist_ui.at(2), 1, 2);
theLayout->addWidget(btnlist_ui.at(3), 1, 3);

//theLayout->setVerticalSpacing(50);
//theLayout->setHorizontalSpacing(10);
theLayout->setContentsMargins(0,120,0,0);//(int left, int top, int right, int bottom)
//theLayout->setColumnStretch(1,22);
theLayout->setSizeConstraint(QLayout::SetMinimumSize);
  setLayout(theLayout);

  QWidget* widget = new QWidget(this);
  widget->setGeometry(0,0,640,360);
  widget->setLayout(theLayout);

//事件过滤器
foreach (QPushButton *btn, btnlist_ui) {
    btn->installEventFilter(this);
}
}
void Elec_Show::connect_Init()
{
    //内部电磁阀
    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
elec_Show_Inelec *menux = new elec_Show_Inelec(this);
     menux->show();


    });

    //外部电磁阀
    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){

        elec_Show_Outelec *menux = new elec_Show_Outelec(this);
             menux->show();

    });

    //三通阀
    connect(btnlist_ui.at(2),&QPushButton::clicked,[=](){

        elec_Show_3elec *menux = new elec_Show_3elec(this);
             menux->show();

    });
    //返回
    connect(btnlist_ui.at(3),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
    });


}
bool Elec_Show::eventFilter(QObject *watched, QEvent *event)
{

//            key_Event::key_Event1(pos,watched, event, btnlist, btnlist_ui);
        for(int i=0;i<btnlist_ui.length();i++)
            if(btnlist_ui.at(i) == watched)
        {
            if(event->type() == QEvent::KeyPress)
            {
               QKeyEvent *ev = (QKeyEvent *)event;
               QPushButton *button = (QPushButton *)watched;
               switch (ev->key())
               {
               case OK:
                 {
                   emit  button->clicked();
                   break;
                 }
               case UP://上
               {
                   if(focusWidget() != btnlist_ui.first())
                   focusPreviousChild();
                   focusPreviousChild();
                   focusPreviousChild();
                   focusPreviousChild();
                   break;
               }
               case DOWN://下
               {
                   if(focusWidget() != btnlist_ui.back())
                   focusNextChild();
                   focusNextChild();
                   focusNextChild();
                   focusNextChild();
                   break;
               }
               case LEFT://左
               {
                 if(focusWidget() != btnlist_ui.first())
                      focusPreviousChild();
                 break;
               }
               case RIGHT://右
               {
                 if(focusWidget() != btnlist_ui.back())
                      focusNextChild();
              break;
               }
               default:
                   break;
               }
            }
            else if (event->type()==QEvent::FocusIn)     //然后再判断控件的具体事件 (这里指获得焦点事件)
                      {
                          QPushButton *focus_button = (QPushButton *)watched;
                                  focus_button->setFixedSize(160,120);
//                                  focus_button->setStyleSheet(focusBtn[2]->styleSheet());
//                                  focus_button->setIconSize(QSize(1,2));
                      }
            else if (event->type()==QEvent::FocusOut)    // 这里指 控件的失去焦点事件
                     {
                QPushButton *focus_button = (QPushButton *)watched;
                        focus_button->setFixedSize(120,90);
                     }
            }
        return QWidget::eventFilter(watched,event);//返回父类的事件过滤器

}
